package com.yehongyu.mytools.ui.swing;

//: ButtonGroups.java
//Uses reflection to create groups of different
//types of AbstractButton.
import java.lang.reflect.Constructor;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
import javax.swing.border.TitledBorder;

public class ButtonGroups extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static String[] ids = { "June", "Ward", "Beaver", "Wally", "Eddie",
			"Lumpy", };

	static JPanel makeBPanel(Class<?> bClass, String[] ids) {
		ButtonGroup bg = new ButtonGroup();
		JPanel jp = new JPanel();
		String title = bClass.getName();
		title = title.substring(title.lastIndexOf('.') + 1);
		jp.setBorder(new TitledBorder(title));
		for (int i = 0; i < ids.length; i++) {
			AbstractButton ab = new JButton("failed");
			try {
				// Get the dynamic constructor method
				// that takes a String argument:
				Constructor<?> ctor = bClass
						.getConstructor(new Class[] { String.class });
				// Create a new object:
				ab = (AbstractButton) ctor.newInstance(new Object[] { ids[i] });
			} catch (Exception ex) {
				System.out.println("can't create " + bClass);
			}
			bg.add(ab);
			jp.add(ab);
		}
		return jp;
	}

	public ButtonGroups() {
		add(makeBPanel(JButton.class, ids));
		add(makeBPanel(JToggleButton.class, ids));
		add(makeBPanel(JCheckBox.class, ids));
		add(makeBPanel(JRadioButton.class, ids));
	}

	public static void main(String args[]) {
		Show.inFrame(new ButtonGroups(), 500, 300);
	}
} // /:~
